package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.activity.tomb.config.TombConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.business.TombGameBusiness;
import com.yanqu.road.server.gameplayer.module.activity.tomb.entity.DbTombInfo;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

//秦始皇陵秦俑再现黏土回收
public class TombRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        TombConfig tombConfig = TombMgr.getConfig();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (tombConfig != null && activityInfo != null) {
            long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(activityInfo.getActivityId()));
            list.add(activityInfo.getActivityName());
            list.add(activityInfo.getServerListStr());
            ActivityItemRecycleMgr.addRecycleItemData(tombConfig.getConfig().getTOMB_REPAIR_ITEM_ID(), activityType, 1, recycleTime, list);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.Tomb.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if (recycleType == 1) {      //秦俑在世黏土自动使用
            try {
                recycleNianTuItem(userId, data);

            } catch (Exception e) {
                LogManager.getLogger(TombRecycleStrategy.class.getName()).error("TombRecycleStrategy recycleNianTuItem", e);
            }

            try {
                sendTakeCareItem(userId, data);
            } catch (Exception e) {
                LogManager.getLogger(TombRecycleStrategy.class.getName()).error("TombRecycleStrategy sendTakeCareItem", e);
            }
        }
    }

    private void recycleNianTuItem(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        Property property = new Property();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        long itemNum = 0;
        String language = UserMgr.getLanguage(userId);
        if (userBagItem != null && userBagItem.getNum() > 0) {
            itemNum = userBagItem.getNum();
            for (int i = 0; i < userBagItem.getNum(); i++) {
                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(userBagItem.getGoodsId());
                property.addProperty(openGoodsBagResult.getReward());
            }
            removeItem(userBagItem);
        }
        if (property.getGoods().size() > 0) {
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TOMB_CLAY_RECYCLE_CONTENT, language), itemNum),
                    MultipleLanguageMgr.getContent(MailManager.TOMB_CLAY_RECYCLE_TITLE, language));
        }

    }

    /**
     * 补发玩家在秦皇陵里打理时的普通道具
     *
     * @param userId
     * @param data
     */

    private void sendTakeCareItem(long userId, ActivityItemRecycleData data) {
        if (data.getParamList() == null || data.getParamList().size() == 0) {
            return;
        }
        String s = data.getParamList().get(0);
        int activityId = Integer.parseInt(s);
        if (activityId == 0) {
            return;
        }
        String activityName = data.getParamList().get(1);
        String serverListStr = data.getParamList().get(2);
        DbTombInfo dbTombInfo;

        // 2023年1月22日00:59:04热更
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        if (onlinePlayer != null) {
            dbTombInfo = onlinePlayer.getModule(TombModule.class).getDbTombInfo(activityId);
        } else {
            dbTombInfo = TombGameBusiness.getDbTombInfo(userId, activityId);
        }

        if (dbTombInfo == null) {
            return;
        }
        if (dbTombInfo.getActivityId() != activityId) {
            return;
        }
        if (dbTombInfo.getTakeCareItemMap().size() == 0) {
            return;
        }
        Property property = new Property();
        dbTombInfo.getTakeCareItemMap().forEach((k, v) -> {
            property.addProperty(k, BigInteger.valueOf(v));
        });
        if (property.getGoods().size() > 0) {
            dbTombInfo.getTakeCareItemMap().clear();
            dbTombInfo.refreshUpdateTime();
            TombGameBusiness.saveDbTombInfo(dbTombInfo);
            String language = UserMgr.getLanguage(userId);
            if (activityName.length() > 0) {
                activityName = ServerLanguageMgr.getContent(activityName, language);
            }
            log.info("userId : {} 补发了秦始皇陵打理背包的道具{}", userId, PropertyHelper.parsePropertyToString(property));
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.AUTO_SEND_ACTIVITY_CROSS_RANK_REWARD_TITLE, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.AUTO_SEND_ACTIVITY_CROSS_RANK_REWARD_CONTENT, language), activityName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), mailContent, mailTitle, activityId, serverListStr);
        }

    }

    @Override
    public boolean isActivityRunning() {
        TombConfig tombConfig = TombMgr.getConfig();
        return tombConfig != null && ActivityMgr.activityInTime(TombMgr.getActivityInfo());
    }
}
